package com.lhh.myspringboot.java8;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

import java.util.regex.Pattern;
import java.util.stream.Stream;

public class StreamBuilder {

    public static void main(String[] args) {


         /*
        1.集合接口有两个方法来生成流
         */
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");


        Stream<String> stream1 = strings.stream(); //获取一个串行流
        Stream<String> parallelStream = strings.parallelStream(); //获取一个并行流



        /*
        2.将数组转成流
         */
        Integer[] nums = new Integer[10];
        Stream<Integer> stream2 = Arrays.stream(nums);


        /*
        3.使用Stream中的静态方法：of()、iterate()、generate()
         */
        Stream<Integer> stream3 = Stream.of(1,2,3,4,5,6);

        Stream<Integer> stream31 = Stream.iterate(0, (x) -> x + 2).limit(6);
        stream31.forEach(System.out::println); // 0 2 4 6 8 10

        Stream<Double> stream32 = Stream.generate(Math::random).limit(2);
        stream32.forEach(System.out::println);


        /*
        4.使用 BufferedReader.lines() 方法，将每行内容转成流
         */
        String s = File.separator;
        try {
            BufferedReader reader = new BufferedReader(new FileReader("D:"+s+"devData"+s+"项目测试数据存放"+s+"file"+s+"test_stream.txt"));
            Stream<String> lineStream = reader.lines();
            lineStream.forEach(System.out::println);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        /*
        5.使用 Pattern.splitAsStream() 方法，将字符串分隔成流
         */
        Pattern pattern = Pattern.compile(",");
        Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
        stringStream.forEach(System.out::println);



    }
}
